package com.android.topview.watermelon.account.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.android.topview.watermelon.R
import com.android.topview.watermelon.account.OnLongClickCallback
import com.android.topview.watermelon.account.adapter.AccountRecyclerAdapter.AccountViewHolder
import com.android.topview.watermelon.account.entity.Account
import com.android.topview.watermelon.application.appContext
import com.android.topview.watermelon.util.showToast

class AccountRecyclerAdapter(var list: List<Account>, var callback: OnLongClickCallback) :
    RecyclerView.Adapter<AccountViewHolder>() {

    fun removeItem(pos: Int) {
        (list as (ArrayList)).removeAt(pos)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccountViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_account, parent, false)
        return AccountViewHolder(view)
    }

    @Suppress("SetTextI18n")
    override fun onBindViewHolder(holder: AccountViewHolder, position: Int) {
        val account = list[position]
        holder.tvDate.text = account.date
        holder.tvDescription.text = account.description
        holder.tvMoney.text = "￥${account.money}"

        holder.tvDelete.setOnLongClickListener {
            callback.longClick(it, position)
            true
        }
        holder.tvDelete.setOnClickListener {
            appContext.showToast("长按可以删除记录")
        }
    }

    override fun getItemCount(): Int = list.size

    inner class AccountViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvDelete: TextView = view.findViewById(R.id.tv_item_delete)
        val tvDate: TextView = view.findViewById(R.id.tv_item_date)
        val tvDescription: TextView = view.findViewById(R.id.tv_item_description)
        val tvMoney: TextView = view.findViewById(R.id.tv_item_money)
    }

}